﻿using DG.Tweening;
using UnityEngine;
using UnityEngine.UI;

namespace BToolkit.ExeUtils
{
    public class Menu : MonoBehaviour
    {
        public RawImage selected, border;
        public BButton mask;
        public RawImage photo;
        public Text nameCN, nameEN;

        public RectTransform rectTransform { get { return transform as RectTransform; } }
        public int index { get; private set; }
        public float size { get; set; }
        public float angle { get; set; }
        MenuScroll menuScroll;
        Color menu1Color, menu2Color;

        private void OnDestroy()
        {
            if (menuScroll) menuScroll._menuSelect -= OnMenuSelect;
        }

        private void Awake()
        {
            nameCN.text = "";
            nameEN.text = "";

            /**
            nameCN.gameObject.SetActive(Config.Instance.ShowMenuName);
            nameEN.gameObject.SetActive(Config.Instance.ShowMenuName);

            nameCN.fontSize = Config.Instance.MenuName1Size;
            nameCN.resizeTextMaxSize = Config.Instance.MenuName1Size;
            menu1Color = BUtils.Color(Config.Instance.MenuName1Color);
            nameCN.color = menu1Color;
            nameCN.rectTransform.anchoredPosition = new Vector2(0, Config.Instance.MenuName1Y);

            nameEN.fontSize = Config.Instance.MenuName2Size;
            nameEN.resizeTextMaxSize = Config.Instance.MenuName2Size;
            menu2Color = BUtils.Color(Config.Instance.MenuName2Color);
            nameEN.color = menu2Color;
            nameEN.rectTransform.anchoredPosition = new Vector2(0, Config.Instance.MenuName2Y);
            */
        }

        public void Init(MenuScroll menuScroll, int index, float size)
        {
            this.menuScroll = menuScroll;
            SetSize(size);
            menuScroll._menuSelect += OnMenuSelect;
            SetIndex(index);
        }

        public void SetSize(float size)
        {
            selected.rectTransform.sizeDelta = new Vector2(size, size);
            size -= menuScroll.selectedSize;
            border.rectTransform.sizeDelta = new Vector2(size, size);
            size -= menuScroll.borderSize;
            mask.rectTransform.sizeDelta = new Vector2(size, size);
            RefreshPhotoSize();
        }

        private void Update()
        {
            if (MenuScroll.currSelectIndex != index)
            {
                if (Mathf.Abs(angle - 90) < menuScroll.angleSpace * 0.5f)
                {
                    menuScroll._menuSelect?.Invoke(this);
                }
            }
        }

        void OnMenuSelect(Menu menu)
        {
            if (menu == this)
            {
                selected.transform.DOKill();
                selected.transform.DOScale(Vector3.one * 1.05f, 0.2f).OnComplete(() =>
                {
                    selected.transform.DOScale(Vector3.one, 0.2f);
                });
            }
            else
            {
                selected.transform.DOKill();
                selected.transform.DOScale(Vector3.one * 0.9f, 0.3f);
            }
        }

        public void SetIndex(int index)
        {
            this.index = index;
            photo.texture = MenuScroll.textures[index];
            nameCN.text = MenuScroll.nameCNs[index];
            nameEN.text = MenuScroll.nameENs[index];
            RefreshPhotoSize();
        }

        public void SetScale(float scale)
        {
            transform.localScale = Vector3.one * scale;
        }

        public void SetAlpha(float alpha)
        {
            if (selected.color.a != alpha)
            {
                selected.color = new Color(1, 1, 1, alpha);
                border.color = new Color(1, 1, 1, alpha);
                photo.color = new Color(1, 1, 1, alpha);
                nameCN.color = new Color(menu1Color.r, menu1Color.g, menu1Color.b, alpha);
                nameEN.color = new Color(menu2Color.r, menu2Color.g, menu2Color.b, alpha);
            }
        }

        void RefreshPhotoSize()
        {
            if (photo.texture)
            {
                Vector2 size;
                if (photo.texture.width > photo.texture.height)
                {
                    size.y = mask.rectTransform.sizeDelta.y;
                    size.x = size.y * photo.texture.width / (float)photo.texture.height;
                }
                else
                {
                    size.x = mask.rectTransform.sizeDelta.x;
                    size.y = size.x * photo.texture.height / (float)photo.texture.width;
                }
                photo.rectTransform.sizeDelta = size;
            }
        }
    }
}